%Usicng Bootstrap method with 500 samples to test MA rules
%input: daily returns, number of days of short term, number of days of long term, band
%output: the fractions of the 500 simulations that result in larger statistics than those for real data
function [ARparameters ARparaTStat ARfractions GARparameters GARparaTStat GARfractions EGARparameters EGARparaTStat EGARfractions] = testMABootstrap(daily_price, s_term, l_term, band, option, day_minmax, test_type)
    %Check if the MAtest is FMA, VMA or TRB
    %If test_type = 'F', the test is FMA test, if type = 'V', the test is VMA test, if type = 'T', the test is TRB test
    if test_type == 'F'
        MAtest = @testFLMA;
    elseif test_type == 'V'
        MAtest = @testVLMA;
    elseif test_type == 'T'
        MAtest = @testTRB;
    end
        
    %Calculate the statistics: mean buy, mean sell, mean buy-sell difference, conditional standard deviations for buy and sell returns
    [r, rbuy, rsell] = MAtest(daily_price, s_term, l_term, band, option, day_minmax);
    rbuy = rbuy((isnan(rbuy)==0));
    rsell = rsell((isnan(rsell)==0));
    buy = mean(rbuy);
    sell = mean(rsell);
    buy_sell = buy - sell;
    b_sig = std(rbuy);
    s_sig = std(rsell);
    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %Use AR model to simulate data based on daily returns
    ari = arima(1,0,0);
    [ari vc1] = estimate(ari,r);
    ARsim = simulate(ari,length(r),'numPaths',500);
    ARparameters = [ari.Constant ari.AR];
    ARparaTStat =  [ari.Constant/sqrt(vc1(1,1)) cell2mat(ari.AR)/sqrt(vc1(2,2))];
    
    count = [0 0 0 0 0];
    
    %count the number of simulations which have the statistics larger than real data's
    for i = 1:500
       [s_r,s_rbuy,s_rsell] = MAtest(ARsim(:,i), s_term, l_term, band, option, day_minmax);
       for j=1:length(s_rbuy)
           if (isnan(s_rbuy(j))==0)
               s_rbuy(j) = ARsim(j,i);
           end
       end
       s_rbuy = s_rbuy((isnan(s_rbuy)==0));
       for j=1:length(s_rsell)
           if (isnan(s_rsell(j))==0)
               s_rsell(j) = ARsim(j,i);
           end
       end
       s_rsell = s_rsell((isnan(s_rsell)==0));
       s_buy = mean(s_rbuy);
       s_sell = mean(s_rsell);
       s_buy_sell = s_buy - s_sell;
       s_b_sig = std(s_rbuy);
       s_s_sig = std(s_rsell);
       if s_buy > buy
           count(1) = count(1) + 1;         
       end
       if s_sell > sell
           count(2) = count(2) +1;
       end
       if s_buy_sell > buy_sell
           count(3) = count(3) + 1;
       end
       if s_b_sig > b_sig
           count(4) = count(4) + 1;
       end
       if s_s_sig > s_sig
           count(5) = count(5) + 1;
       end                       
    end
    
    %calculate the fractions
    ARfractions = count./500;    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
    %Use GARCH model to simulate data based on daily returns
    gar = garch(1,1);
    [gar vc2] = estimate(gar,r);
    [V,GARsim] = simulate(gar,length(r),'numPaths',500);
    GARparameters = [gar.Constant gar.GARCH gar.ARCH];
    GARparaTStat =  [gar.Constant/sqrt(vc2(1,1)) cell2mat(gar.GARCH)/sqrt(vc2(2,2)) cell2mat(gar.ARCH)/sqrt(vc2(3,3))];

    
    count = [0 0 0 0 0];
    
    %count the number of simulations which have the statistics larger than real data's
    for i = 1:500
       [s_r,s_rbuy,s_rsell] = MAtest(GARsim(:,i), s_term, l_term, band, option, day_minmax);
       for j=1:length(s_rbuy)
           if (isnan(s_rbuy(j))==0)
               s_rbuy(j) = GARsim(j,i);
           end
       end
       s_rbuy = s_rbuy((isnan(s_rbuy)==0));
       for j=1:length(s_rsell)
           if (isnan(s_rsell(j))==0)
               s_rsell(j) = GARsim(j,i);
           end
       end
       s_rsell = s_rsell((isnan(s_rsell)==0));
       s_buy = mean(s_rbuy);
       s_sell = mean(s_rsell);
       s_buy_sell = s_buy - s_sell;
       s_b_sig = std(s_rbuy);
       s_s_sig = std(s_rsell);
       if s_buy > buy
           count(1) = count(1) + 1;         
       end
       if s_sell > sell
           count(2) = count(2) +1;
       end
       if s_buy_sell > buy_sell
           count(3) = count(3) + 1;
       end
       if s_b_sig > b_sig
           count(4) = count(4) + 1;
       end
       if s_s_sig > s_sig
           count(5) = count(5) + 1;
       end                       
    end
    
    %calculate the fractions
    GARfractions = count./500;  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %Use EGARCH model to simulate data based on daily returns
    egar = egarch(1,1);
    [egar vc3] = estimate(egar,r);
    [V,EGARsim] = simulate(egar,length(r),'numPaths',500);
    EGARparameters = [egar.Constant egar.ARCH egar.GARCH egar.Leverage];
    EGARparaTStat =  [egar.Constant/sqrt(vc3(1,1)) cell2mat(egar.GARCH)/sqrt(vc3(2,2)) cell2mat(egar.ARCH)/sqrt(vc3(3,3)) cell2mat(egar.Leverage)/sqrt(vc3(4,4))];

    count = [0 0 0 0 0];
    
    %count the number of simulations which have the statistics larger than real data's
    for i = 1:500
       [s_r,s_rbuy,s_rsell] = MAtest(EGARsim(:,i), s_term, l_term, band, option, day_minmax);
       for j=1:length(s_rbuy)
           if (isnan(s_rbuy(j))==0)
               s_rbuy(j) = EGARsim(j,i);
           end
       end
       s_rbuy = s_rbuy((isnan(s_rbuy)==0));
       for j=1:length(s_rsell)
           if (isnan(s_rsell(j))==0)
               s_rsell(j) = EGARsim(j,i);
           end
       end
       s_rsell = s_rsell((isnan(s_rsell)==0));
       s_buy = mean(s_rbuy);
       s_sell = mean(s_rsell);
       s_buy_sell = s_buy - s_sell;
       s_b_sig = std(s_rbuy);
       s_s_sig = std(s_rsell);
       if s_buy > buy
           count(1) = count(1) + 1;         
       end
       if s_sell > sell
           count(2) = count(2) +1;
       end
       if s_buy_sell > buy_sell
           count(3) = count(3) + 1;
       end
       if s_b_sig > b_sig
           count(4) = count(4) + 1;
       end
       if s_s_sig > s_sig
           count(5) = count(5) + 1;
       end                       
    end
    
    %calculate the fractions
    EGARfractions = count./500;    
end